package com.situ.studentmanagement.controller.api;

import com.situ.studentmanagement.Constants;
import com.situ.studentmanagement.Utils;
import com.situ.studentmanagement.dao.AdminMapper;
import com.situ.studentmanagement.dao.ClassMapper;
import com.situ.studentmanagement.dao.StudentMapper;
import com.situ.studentmanagement.model.Admin;
import com.situ.studentmanagement.model.CheckInfo;
import com.situ.studentmanagement.model.Student;
import org.apache.ibatis.binding.BindingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by baba on 2016/7/13.
 */
@RestController
@RequestMapping("/api/admin")
public class AdminController {
    @Autowired
    AdminMapper adminMapper;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    ClassMapper classMapper;

    @RequestMapping(value = "/login")
    public Map login(HttpSession session,
                     @RequestParam("name") String name,
                     @RequestParam("password") String password) {
        Map map = new HashMap();
        try {
            //直接使用这个mapper即可，不用自己去创建了
            Admin a = this.adminMapper.login2(name, password);
            //把id保存到session中
            //session.setAttribute(Constants.ADMIN_LOGIN_ATTR_ID, s.getId());
            map.put("result", 1);
            map.put("admin", a);
        } catch (Exception e) {
            map.put("result", 0);
            map.put("errorMsg", e.getLocalizedMessage());
        }
        return map;
    }

    //获取非正常的考勤信息，指那些迟到的，旷课的等
    @RequestMapping(value = "/getAbnormalCheckInfos",method = RequestMethod.POST)
    public Map getAbnormalCheckInfos(HttpSession session,
                                     @RequestParam("admin_id") int admin_id) {
        Map map = new HashMap();

        //把当天的不正常打卡的学生信息都取出来
        //check_log表中，打了卡就有记录，没打卡就没记录
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now =new Date();
        Calendar calendar=Calendar.getInstance();

        //把时分秒置成0再加上上班时间的秒数，就是今天的上班时间点
        Date dayBegin = Utils.getDayBeginTime(now);// calendar.getTime();//记下一天开始的时间

        //形成上课时间
        calendar.setTime(dayBegin);
        calendar.add(Calendar.SECOND,Constants.CLASS_BEGIN_TIME);
        Date classBeginTime=calendar.getTime();

        //形成下课时间
        calendar.setTime(dayBegin);
        calendar.add(Calendar.SECOND,Constants.CLASS_OVER_TIME);
        Date classOverTime=calendar.getTime();

        //形成旷课开始时间
        calendar.setTime(dayBegin);
        calendar.add(Calendar.SECOND,Constants.ABSENCE_TIME);
        Date absenceTime=calendar.getTime();

        //形成各时间点的字符串
        String classBeginTimeStr = sdf.format(classBeginTime);
        String classOverTimeStr =sdf.format(classOverTime);
        String absenceTimeStr=sdf.format(absenceTime);
        String nowStr = sdf.format(now);

        //取所有迟到的人（如果当前时间过了上班时间，但未到旷工时间，取所有过了上班时间才打卡的人们）
        //取得所有在上班之后到当前时间点之前的所有打卡记录
        List<CheckInfo> latedChecks = studentMapper.getCheckInfosFromTimeToTime(classBeginTimeStr,absenceTimeStr);
        //还得把每条的状态搞对了
        Utils.setStatusToLated(latedChecks);
        map.put("latedChecks",latedChecks);

        //取所有旷工的人（如果当前时间过了旷工时间，取所有过了旷工时间才打卡的人）
        List<CheckInfo> absencedChecks = studentMapper.getCheckInfosFromTimeToTime(absenceTimeStr,nowStr);
        Utils.setStatusToLabsenced(latedChecks);
        map.put("absencedChecks",absencedChecks);

        //取到当前时间为止(从上班之后开始)，所有未打卡的人
        List<CheckInfo> noChecks = studentMapper.getNoCheckInfosFromTimeToTime(classBeginTimeStr,nowStr);
        map.put("noChecks",noChecks);
        map.put("result", 1);
        return map;
    }

    @RequestMapping("/deletClass")
    public Map deletClass(Model model,
                          @RequestParam("id")int id) {
        classMapper.deleteClassById(id);
        Map map=new HashMap();
        map.put("deleId",id);
        return map;
    }
}
